module;
#include <QObject>
#include <QMap>
#include <QString>
#include <QMutex>
#include <QtCore>
#include <QtCore/qtmochelpers.h>
#include <QtCore/QPair>
#include <QThread>
module ModbusClientThread;
import XYmodbus;

ModbusClientThread::ModbusClientThread(QObject *parent)
    : QThread(parent), modbusClientDevice(nullptr)
{
    modbusClientDevice = new ModbusClientHandle(this);
}

ModbusClientThread::~ModbusClientThread()
{
    if (modbusClientDevice) {
        modbusClientDevice->disconnect();
        modbusClientDevice->deleteLater();
        delete modbusClientDevice;
        modbusClientDevice = nullptr; // 置空指针
    }

}

void ModbusClientThread::run()
{

    //modbusClientDevice = new ModbusClientHandle(this);
    // 直接连接信号
    connect(modbusClientDevice, &ModbusClientHandle::ModbusStateChanged, this, &ModbusClientThread::modbusStateChanged, Qt::QueuedConnection);
    connect(modbusClientDevice, &ModbusClientHandle::readCompleted, this, &ModbusClientThread::readCompleted, Qt::QueuedConnection);
    connect(modbusClientDevice, &ModbusClientHandle::readCompletedChanged, this, &ModbusClientThread::readCompletedChanged, Qt::QueuedConnection);
    connect(modbusClientDevice, &ModbusClientHandle::allReadCompleted, this, &ModbusClientThread::allReadCompleted, Qt::QueuedConnection);
    connect(modbusClientDevice, &ModbusClientHandle::writeCompleted, this, &ModbusClientThread::writeCompleted, Qt::QueuedConnection);
    connect(modbusClientDevice, &ModbusClientHandle::allWritesCompleted, this, &ModbusClientThread::allWritesCompleted, Qt::QueuedConnection);

    // 连接新增的信号到 ModbusClientHandle 的槽方法
    connect(this, &ModbusClientThread::loadConfigSignal, modbusClientDevice, &ModbusClientHandle::loadConfigSlot, Qt::QueuedConnection);
    connect(this, &ModbusClientThread::connectToDeviceSignal, modbusClientDevice, &ModbusClientHandle::connectToDeviceSlot, Qt::QueuedConnection);
    connect(this, &ModbusClientThread::disconnectToDeviceSignal, modbusClientDevice, &ModbusClientHandle::disconnectToDeviceSlot, Qt::QueuedConnection);
    connect(this, &ModbusClientThread::readModbusValuesSignal, modbusClientDevice, &ModbusClientHandle::readModbusValuesSlot, Qt::QueuedConnection);
    connect(this, &ModbusClientThread::writeModbusValuesSignal, modbusClientDevice, &ModbusClientHandle::writeModbusValuesSlot, Qt::QueuedConnection);

    exec();
}



//放在文件最后，文件需要手动生成
#include "modbusClientThread.moc"
